package cool.doudou.doudada.iot.codec.core.adapter.impl;

import com.alibaba.fastjson2.JSONObject;
import cool.doudou.doudada.iot.codec.core.adapter.ICodecAdapter;

import java.util.Base64;

/**
 * 编解码适配器 默认实现
 *
 * @author jiangcs
 * @since 2022/09/30
 */
public class DefaultCodecAdapterImpl implements ICodecAdapter {
    @Override
    public String getManufactureId() {
        return "default";
    }

    @Override
    public String productId() {
        return "default";
    }

    @Override
    public JSONObject decode(byte[] bytes) throws Exception {
        if (bytes == null || bytes.length <= 0) {
            throw new IllegalArgumentException("参数bytes为空");
        }
        return JSONObject.of("data", Base64.getEncoder().encodeToString(bytes));
    }

    @Override
    public byte[] encode(JSONObject paramObj) throws Exception {
        if (paramObj == null) {
            throw new IllegalArgumentException("参数paramObj为空");
        }
        String dataStr = paramObj.getString("data");
        if (dataStr == null || "".equals(dataStr)) {
            throw new IllegalArgumentException("参数data为空");
        }
        return Base64.getDecoder().decode(dataStr);
    }
}
